package com.icbc.springcloud.controller;

import com.icbc.springcloud.pojo.Dept;
import com.icbc.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//提供restful服务
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService service;

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGetDept")//失败了去调用备用方法
    public Dept getDept(@PathVariable("id")int id){
        Dept dept=new Dept();
        dept=service.queryById(id);
        if(dept==null){
            throw  new RuntimeException("id=>"+id+",不存在该用户，或者信息无法找到~");
        }
        return service.queryById(id);
    }

    //备选方法,熔断方法
    public Dept hystrixGetDept(@PathVariable("id") int id){
        Dept dept=new Dept();
        dept.setDeptno(id);
        dept.setDname("id=>"+id+"，没有对应的信息");
        dept.setDb_source("no dataBase in MySQL");
        return dept;
    }
}
